﻿using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;

namespace Librerias
{
    public class Encriptador
    {

        public static string Encrypt(string plainText,
                                     string passPhrase,
                                     string saltValue,
                                     string hashAlgorithm,
                                     int passwordIterations,
                                     string initVector,
                                     int keySize)
        {

            byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
            byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);

            byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);

            var password = new PasswordDeriveBytes(
                passPhrase,
                saltValueBytes,
                hashAlgorithm,
                passwordIterations);

#pragma warning disable 612,618
            byte[] keyBytes = password.GetBytes(keySize / 8);
#pragma warning restore 612,618

        
            var symmetricKey = new RijndaelManaged {Mode = CipherMode.CBC};


            ICryptoTransform encryptor = symmetricKey.CreateEncryptor(
                keyBytes,
                initVectorBytes);

        
            var memoryStream = new MemoryStream();

        
            var cryptoStream = new CryptoStream(memoryStream,
                                                         encryptor,
                                                         CryptoStreamMode.Write);
            // Start 
            cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);

            // Finish 
            cryptoStream.FlushFinalBlock();

    
            byte[] cipherTextBytes = memoryStream.ToArray();

     
            memoryStream.Close();
            cryptoStream.Close();

      
            string cipherText = Convert.ToBase64String(cipherTextBytes);

      
            return cipherText;
        }

        public static string Decrypt(string cipherText,
                                     string passPhrase,
                                     string saltValue,
                                     string hashAlgorithm,
                                     int passwordIterations,
                                     string initVector,
                                     int keySize)
        {

            byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
            byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);

        
            byte[] cipherTextBytes = Convert.FromBase64String(cipherText);


            var password = new PasswordDeriveBytes(
                passPhrase,
                saltValueBytes,
                hashAlgorithm,
                passwordIterations);


#pragma warning disable 612,618
            byte[] keyBytes = password.GetBytes(keySize / 8);
#pragma warning restore 612,618


            var symmetricKey = new RijndaelManaged {Mode = CipherMode.CBC};


            ICryptoTransform decryptor = symmetricKey.CreateDecryptor(
                keyBytes,
                initVectorBytes);

        
            var memoryStream = new MemoryStream(cipherTextBytes);

        
            var cryptoStream = new CryptoStream(memoryStream,
                                                         decryptor,
                                                         CryptoStreamMode.Read);


            var plainTextBytes = new byte[cipherTextBytes.Length];

        
            int decryptedByteCount = cryptoStream.Read(plainTextBytes,
                                                       0,
                                                       plainTextBytes.Length);

        
            memoryStream.Close();
            cryptoStream.Close();

        
            string plainText = Encoding.UTF8.GetString(plainTextBytes,
                                                       0,
                                                       decryptedByteCount);

       

            return plainText;
        }
    }
}
